package com.xiaoyu.lanling.feature.moment

import android.os.Bundle
import android.view.View
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.moment.notice.CommentNoticeReadStateEvent
import com.xiaoyu.lanling.event.moment.notice.LikeNoticeReadStateEvent
import com.xiaoyu.lanling.feature.moment.fragment.notice.CommentNoticeFragment
import com.xiaoyu.lanling.feature.moment.fragment.notice.LikeNoticeFragment
import com.xiaoyu.lanling.view.FragmentAdapter
import com.xiaoyu.lanling.view.OnShowFragment
import com.xiaoyu.lanling.widget.tablayout.ScaleTabLayoutUtils
import kotlinx.android.synthetic.main.activity_moment_notice.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MomentNoticeActivity : AppCompatToolbarActivity() {

    companion object {
        const val PAGE_INDEX_COMMENT = 0
        const val PAGE_INDEX_LIKE = 1
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLightStatusBar()
        setContentView(R.layout.activity_moment_notice)
        initView()
        initEvent()
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: LikeNoticeReadStateEvent) {
                like_notice_dot.visibility = if (event.isRead) View.INVISIBLE else View.VISIBLE
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: CommentNoticeReadStateEvent) {
                comment_notice_dot.visibility = if (event.isRead) View.INVISIBLE else View.VISIBLE
            }
        })
    }

    private fun initView() {
        initToolbar()
        title = AppContext.getString(R.string.moment_remind)

        val initIndex = intent.getIntExtra(Constant.IntentKey.KEY_NOTICE_PAGE_INDEX, PAGE_INDEX_COMMENT)
        val fragmentList = mutableListOf<OnShowFragment>()
        fragmentList.add(CommentNoticeFragment())
        fragmentList.add(LikeNoticeFragment())
        val titles = listOf(AppContext.getString(R.string.moment_comment), AppContext.getString(R.string.moment_like))

        ScaleTabLayoutUtils.createNoScaleTabLayout(magic_indicator, view_pager, titles, true)
        val fragmentAdapter = FragmentAdapter(supportFragmentManager, fragmentList, titles)
        view_pager.offscreenPageLimit = fragmentList.size
        view_pager.adapter = fragmentAdapter
        view_pager.currentItem = initIndex
    }
}